package open_graphics_library;
/*
 * The initial code is based on the FullscreenExample and
 *   spaceinvedaers from the lwjgl.org
 */

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

// Is this where I define the GL version? e.g. GLES20?
import static org.lwjgl.opengl.GL11.*;

public class OpenGraphicsLibrary {

	public OpenGraphicsLibrary() {
		// Try to make the 'true' a var m_defaultIsApplication
		this(800, 600, 60, true);
	}

	// TODO C do a synced update.
	// TODO C find out if it is a wide screen or not.
	// TODO C make the hieght and width available to the game class.
	public OpenGraphicsLibrary(int nPixelWidth, int nPixelHeight, int nSyncFrequency,
			Boolean isApplication) {
		try {
			Display.setDisplayMode(new DisplayMode(800, 600));
			Display.create();
			setDisplayMode(800, 600, true);

			// grab the mouse, dont want that hideous cursor when we're playing!
			if (isApplication) {
				Mouse.setGrabbed(true);
			}

			// enable textures since we're going to use these for our sprites
			glEnable(GL_TEXTURE_2D);

			// disable the OpenGL depth test since we're rendering 2D graphics
			glDisable(GL_DEPTH_TEST);

			glMatrixMode(GL_PROJECTION);
			glLoadIdentity();

			glOrtho(0, nPixelWidth, nPixelHeight, 0, -1, 1);
			glMatrixMode(GL_MODELVIEW);
			glLoadIdentity();
			glViewport(0, 0, nPixelWidth, nPixelHeight);
		} catch (LWJGLException e) {
			e.printStackTrace();
			System.exit(0);
		}

	}

	/**
	 * 
	 * @param width
	 *            - in pixels
	 * @param height
	 *            - in pixels
	 * @param fullscreen
	 *            - boolean
	 */
	private void setDisplayMode(int width, int height, boolean fullscreen) {
		// return if requested DisplayMode is already set
		if ((Display.getDisplayMode().getWidth() == width)
				&& (Display.getDisplayMode().getHeight() == height)
				&& (Display.isFullscreen() == fullscreen)) {
			return;
		}

		try {
			DisplayMode targetDisplayMode = null;

			if (fullscreen) {
				DisplayMode[] modes = Display.getAvailableDisplayModes();
				int freq = 0;

				for (int i = 0; i < modes.length; i++) {
					DisplayMode current = modes[i];

					if ((current.getWidth() == width)
							&& (current.getHeight() == height)) {
						if ((targetDisplayMode == null)
								|| (current.getFrequency() >= freq)) {
							if ((targetDisplayMode == null)
									|| (current.getBitsPerPixel() > targetDisplayMode
											.getBitsPerPixel())) {
								targetDisplayMode = current;
								freq = targetDisplayMode.getFrequency();
							}
						}

						// if we've found a match for bpp and frequence against
						// the
						// original display mode then it's probably best to go
						// for this one
						// since it's most likely compatible with the monitor
						if ((current.getBitsPerPixel() == Display
								.getDesktopDisplayMode().getBitsPerPixel())
								&& (current.getFrequency() == Display
										.getDesktopDisplayMode().getFrequency())) {
							targetDisplayMode = current;
							break;
						}
					}
				}
			} else {
				targetDisplayMode = new DisplayMode(width, height);
			}

			if (targetDisplayMode == null) {
				System.out.println("Failed to find value mode: " + width + "x"
						+ height + " fs=" + fullscreen);
				return;
			}

			Display.setDisplayMode(targetDisplayMode);
			Display.setFullscreen(fullscreen);

		} catch (LWJGLException e) {
			System.out.println("Unable to setup mode " + width + "x" + height
					+ " fullscreen=" + fullscreen + e);
		}

	} // end setDisplayMode.

	public boolean isCloseRequested() {
		return Display.isCloseRequested();
	}

	public void destroyDisplay() {
		Display.destroy();
	}

	public void updateDisplay() {
		Display.update();
	}

	public void syncDisplay(int frequency) {
		// TODO Make the frequency configurable.
		Display.sync(frequency); // cap fps to 60fps
	}

	public void clearScreen() {
		// clear screen
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
	}

	public void setTitle(String windowTitle) {
		Display.setTitle(windowTitle);
		
	}

}
